package com.dianxin.smartpark.service.attendance.impl;

import cn.hutool.core.util.IdUtil;
import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.constant.ErrMsgEnum;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.dianxin.smartpark.dao.dataobject.attendance.SmartParkHolidayDetail;
import com.dianxin.smartpark.dao.dataobject.attendance.SmartParkHolidayDetailExample;
import com.dianxin.smartpark.dao.mapper.attendance.SmartParkHolidayDetailMapper;
import com.dianxin.smartpark.dao.qo.attendance.SmartParkHolidayDetailQO;
import com.dianxin.smartpark.service.attendance.SmartParkHolidayDetailService;
import com.dianxin.smartpark.service.utils.DateTimeUtil;
import com.dianxin.smartpark.service.vo.attendance.SmartParkHolidayDetailVO;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @program: smart-park
 * @description:
 * @author: chenyong
 * @create: 2021-07-20 11:41
 */
@Service
public class SmartParkHolidayDetailServiceImpl implements SmartParkHolidayDetailService {
    private final static Logger logger = LoggerFactory.getLogger(SmartParkHolidayDetailServiceImpl.class);

    @Resource
    private SmartParkHolidayDetailMapper smartParkHolidayDetailMapper;

    /**
     * @Description:
     * @Author: ChenYong
     * @Date: 2021/7/21 0021
     * @param smartParkHolidayDetailQO:
     * @Return: CommonResponseBody<List<SmartParkHolidayDetailVO>>
     */
    @Override
    public CommonResponseBody<List<SmartParkHolidayDetailVO>> getHolidayDetails(SmartParkHolidayDetailQO smartParkHolidayDetailQO) {
        SmartParkHolidayDetailExample recordExample = new SmartParkHolidayDetailExample();
        SmartParkHolidayDetailExample.Criteria criteria = recordExample.createCriteria();
        criteria.andIsDeletedEqualTo(0);
        if (smartParkHolidayDetailQO.getNeedPage()) {
            recordExample.setOrderByClause("gmt_create desc limit " + (smartParkHolidayDetailQO.getPageIndex() - 1) * smartParkHolidayDetailQO.getPageSize() + "," + smartParkHolidayDetailQO.getPageSize());
        } else {
            recordExample.setOrderByClause("gmt_create desc");
        }

        List<SmartParkHolidayDetail> passRecords = smartParkHolidayDetailMapper.selectByExample(recordExample);
        long count = smartParkHolidayDetailMapper.countByExample(recordExample);

        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, smartParkHolidayDetailQO, SmartParkHolidayDetailVO.class);
    }


    /**
     * @Description: 通过日期时间查询查询是否需要打卡（true:需要打卡，false：不需要打卡）
     * @Author: ChenYong
     * @Date: 2021/7/22 0022
     * @param smartParkHolidayDetailQO:
     * @Return: CommonResponseBody
     */
    @Override
    public CommonResponseBody queryByDate(SmartParkHolidayDetailQO smartParkHolidayDetailQO) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        Date date = new Date();
        try {
            date = simpleDateFormat.parse(smartParkHolidayDetailQO.getTimes());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);

            smartParkHolidayDetailQO.setYear(String.valueOf(calendar.get(Calendar.YEAR)));
            int monthInt = calendar.get(Calendar.MONTH) + 1;
            String month = null;
            if (monthInt<10){
                month = "0"+monthInt;
            }else {
                month = ""+monthInt;
            }
            smartParkHolidayDetailQO.setMonth(month);
            smartParkHolidayDetailQO.setDates(month+String.valueOf(calendar.get(Calendar.DATE)));
            smartParkHolidayDetailQO.setTimes(simpleDateFormat.format(date));
            smartParkHolidayDetailQO.setWeek(String.valueOf(DateTimeUtil.getWeekDay(date)));
            Boolean status = null;
            SmartParkHolidayDetail smartParkHolidayDetail = smartParkHolidayDetailMapper.selectByParams(smartParkHolidayDetailQO);
            if(smartParkHolidayDetail!=null){
                if("0".equals(smartParkHolidayDetail.getType())){
                    status = false;
                }
                if("1".equals(smartParkHolidayDetail.getType())){
                    status = true;
                }
            }else {
                if("6".equals(smartParkHolidayDetailQO.getWeek())){
                    status = false;
                } else if("7".equals(smartParkHolidayDetailQO.getWeek())){
                    status = false;
                }else {
                    status = true;
                }
            }
            return ResponseBodyWrapperUtil.successWrapper(status);
        } catch (Exception e) {
            logger.error("", e);
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_SYSTEM.getErrorMessage(),ErrMsgEnum.ERROR_SYSTEM.getErrorCode());
        }
    }

    /**
     * @param smartParkHolidayDetailQO:
     * @Description: 添加节假日信息
     * @Author: ChenYong
     * @Date: 2021/7/21 0021
     * @Return: CommonResponseBody<Boolean>
     */
    @Override
    public CommonResponseBody saveHolidayDetail(SmartParkHolidayDetailQO smartParkHolidayDetailQO) {
        SmartParkHolidayDetail smartParkHolidayDetail = new SmartParkHolidayDetail();
        SmartParkHolidayDetail entity = new SmartParkHolidayDetail();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        Date date = new Date();
        try {
            date = simpleDateFormat.parse(smartParkHolidayDetailQO.getTimes());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);

            smartParkHolidayDetailQO.setYear(String.valueOf(calendar.get(Calendar.YEAR)));
            int monthInt = calendar.get(Calendar.MONTH) + 1;
            String month = null;
            if (monthInt < 10) {
                month = "0" + monthInt;
            } else {
                month = "" + monthInt;
            }
            int dayInt = calendar.get(Calendar.DATE);
            String day = null;
            if (dayInt < 10) {
                day = "0" + dayInt;
            } else {
                day = "" + dayInt;
            }
            smartParkHolidayDetailQO.setMonth(month);
            smartParkHolidayDetailQO.setDates(month+day);
            smartParkHolidayDetailQO.setTimes(simpleDateFormat.format(date));
            smartParkHolidayDetailQO.setWeek(String.valueOf(DateTimeUtil.getWeekDay(date)));
            entity = smartParkHolidayDetailMapper.selectByParams(smartParkHolidayDetailQO);
            if (entity != null && entity.getId() != null) {
                //当日记录存在则不保存
                return ResponseBodyWrapperUtil.fail("当前日期存在节假日","80010");
            } else {
                BeanUtil.conversion(smartParkHolidayDetailQO,smartParkHolidayDetail);
                smartParkHolidayDetail.beforeInsert();
                return ResponseBodyWrapperUtil.toResData(smartParkHolidayDetailMapper.insert(smartParkHolidayDetail));
            }
        } catch (Exception e) {
            logger.error("", e);
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_SYSTEM.getErrorMessage(),ErrMsgEnum.ERROR_SYSTEM.getErrorCode());
        }
    }

    /**
     * @Description:  通过qo查询List
     * @Author: ChenYong
     * @Date: 2021/7/21 0021
     * @param smartParkHolidayDetailQO:
     * @Return: CommonResponseBody<List<SmartParkHolidayDetail>>
     */
    @Override
    public CommonResponseBody<List<SmartParkHolidayDetailVO>> getListAll(SmartParkHolidayDetailQO smartParkHolidayDetailQO) {
        SmartParkHolidayDetailVO smartParkHolidayDetailVO = new SmartParkHolidayDetailVO();
        try {
            List<SmartParkHolidayDetail> smartParkHolidayDetails = smartParkHolidayDetailMapper.getByParams(smartParkHolidayDetailQO);
            return ResponseBodyWrapperUtil.successWrapper(BeanUtil.queryListConversion(smartParkHolidayDetails,SmartParkHolidayDetailVO.class));
        } catch (Exception e) {
            logger.error("", e);
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_SYSTEM.getErrorMessage(),ErrMsgEnum.ERROR_SYSTEM.getErrorCode());
        }
    }

    /**
     * @Description:  删除节假日
     * @Author: ChenYong
     * @Date: 2021/7/29 0029
     * @param smartParkHolidayDetailQO:
     * @Return: CommonResponseBody
     */
    @Override
    public CommonResponseBody deleteHolidayDetail(SmartParkHolidayDetailQO smartParkHolidayDetailQO) {
        SmartParkHolidayDetail smartParkHolidayDetail = smartParkHolidayDetailMapper.selectByPrimaryKey(smartParkHolidayDetailQO.getId());
        smartParkHolidayDetail.setIsDeleted(1);
        smartParkHolidayDetail.beforeDelete();
        return ResponseBodyWrapperUtil.toResData(smartParkHolidayDetailMapper.updateByPrimaryKey(smartParkHolidayDetail));
    }
}
